package com.hnzq.cloud.freshaddressassembly.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hnzq.cloud.dto.AllSiteDto;
import com.hnzq.cloud.dto.OneSiteDto;
import com.hnzq.cloud.freshaddressassembly.domain.FreSite;
import com.hnzq.cloud.freshaddressassembly.domain.FreSiteArea;
import com.hnzq.cloud.freshaddressassembly.service.IFreSiteService;
import com.hnzq.cloud.freshaddressassembly.service.impl.FreSiteAreaServiceImpl;
import com.hnzq.cloud.freshaddressassembly.util.WoniuBeanUtils;
import com.hnzq.cloud.model.Result;
import com.hnzq.cloud.api.SiteQueryApi;
import com.hnzq.cloud.siteParam.AddSiteParam;
import com.hnzq.cloud.siteParam.FindSiteParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author qinle
 * @since 2022-02-10
 */
@RestController
@RequestMapping("/freSite")
public class FreSiteController implements SiteQueryApi {
    @Resource
    private IFreSiteService freSiteService;
    @Resource
    private FreSiteAreaServiceImpl freSiteAreaService;
    //查询单个门店信息
    @GetMapping("/findSite")
    @Override
    public Result<OneSiteDto> findSite(Integer siteId) {
        FreSite freSite = freSiteService.getById(siteId);
        return Result.newInstance().setData(WoniuBeanUtils.copyObject(freSite,OneSiteDto.class));
    }
    @PostMapping("/addSite")
    //新增门店
    @Override
    public Result addSite(AddSiteParam addSiteParam) {
        //先查询出所有站点信息，拿到经纬度
        //将新曾的门店坐标转换为double类型
        Double x = Double.valueOf(addSiteParam.getSiteX());
        Double y = Double.valueOf(addSiteParam.getSiteY());

        //然后挨个比较他们的直线距离如果小于站点的半径，说明该门店属于该站点管理
        List<FreSiteArea> freSiteAreaList = freSiteAreaService.list(new QueryWrapper<FreSiteArea>().eq("is_delete", 0)); //获取所有站点区域信息；
        for (int i = 0; i <= freSiteAreaList.size(); i++) {
            //找到距离小于半径的站点
            if (freSiteAreaList.get(i).getAreaLength() > WoniuBeanUtils.getNewDistance(x, y, Double.valueOf(freSiteAreaList.get(i).getAreaX()), Double.valueOf(freSiteAreaList.get(i).getAreaY()))) {
                //绑上对应的站点标签
                addSiteParam.setAreaId(freSiteAreaList.get(i).getAreaId());
                //新增
                if (freSiteService.save(WoniuBeanUtils.copyObject(addSiteParam, FreSite.class))) {
                    return Result.newInstance().setMessage("新增成功");
                }
                break;
            }
        }
        return Result.newInstance().setMessage("新增失败");
    }
    //删除门店
    @DeleteMapping("/removeSite")
    @Override
    public Result removeSite(Integer siteId) {
        FreSite site = freSiteService.getById(siteId);
        site.setIsDelete(1);
        if (freSiteService.updateById(site)) {
            return Result.newInstance().setMessage("删除成功");
        }else {
            return Result.newInstance().setMessage("删除失败");
        }
    }
    //修改门店信息
    @PutMapping("/updateSite")
    @Override
    public Result updateSite(AddSiteParam addSiteParam) {
        Integer userId = addSiteParam.getUserId();
        FreSite site = freSiteService.getOne(new QueryWrapper<FreSite>().eq("user_id", userId));
        Integer siteId = site.getSiteId();
        FreSite freSite = new FreSite();
        FreSite newsite = WoniuBeanUtils.copyObject(addSiteParam, freSite.getClass());
        newsite.setSiteId(siteId);
        if (freSiteService.saveOrUpdate(newsite)) {
            return Result.newInstance().setMessage("修改成功");
        }else {
            return Result.newInstance().setMessage("修改失败");
        }

    }
    //根据站点区域标签查询门店
    @GetMapping("/findAllSite")
    @Override
    public Result<AllSiteDto> findAllSite(Integer siteAreaId) {
        List<FreSite> siteList = freSiteService.list(new QueryWrapper<FreSite>().eq("area_id", siteAreaId));
        List<OneSiteDto> oneSiteDtos = WoniuBeanUtils.copyList(siteList, OneSiteDto.class);
        AllSiteDto allSiteDto = new AllSiteDto();
        allSiteDto.setSiteDtoList(oneSiteDtos);
        return Result.newInstance().setData(allSiteDto);
    }
    //根据用户定位推送周围自提点
    @GetMapping("/findAroudSite")
    @Override
    public Result<AllSiteDto> findAroudSite(FindSiteParam findSiteParam) {
        AllSiteDto siteDto = new AllSiteDto();
        List<FreSite> siteList = freSiteService.list();
        List<OneSiteDto> siteDtoList = new ArrayList<>();
        System.out.println(siteList);
        for(int i=0;i<siteList.size();i++){
         if(WoniuBeanUtils.getNewDistance(findSiteParam.getX(),findSiteParam.getY(),Double.valueOf(siteList.get(i).getSiteX()),Double.valueOf(siteList.get(i).getSiteY()))-1000.0<0){
             siteDtoList.add(WoniuBeanUtils.copyObject(siteList.get(i),OneSiteDto.class));
             siteDto.setSiteDtoList(siteDtoList);
         }
        }
        return Result.newInstance().setMessage("周围门店").setData(siteDto);
    }

}

